#' ---
#' author: "```matthew.pietryka@gmail.com```"
#' date: "2019-07-26"
#' output:
#'  html_document:
#'    warning: false
#'    toc: true
#'    code_folding: hide
#' ---


#' **PURPOSE:**
#' This script creates plots to display the relationship between distance and text similarity (Figure 2)"




# GET STARTED ---------------

#  load packages
library(tidyverse)


#  load data and plotting preferences
source("SC-Plots-Load-the-Data.R")
source("SC-Plots-Preferences.R")



# SCATTER PLOT ---------------------

x_lab <- "Distance between states (Z score)"

scatter_plot <- df_new  %>%
  filter(same_state == 0)  %>%
  filter(state_code_from != "USA")  %>%
  filter(state_code_to != "USA")  %>%
  mutate(share_border_lab = recode(share_border,
                                   `1` = "Shared border",
                                   `0` = "No shared border"))  %>%
  ggplot(aes(x = scale(distance), y = ratio, color = share_border_lab,
             alpha = share_border_lab)) +
  geom_point(size = 3) +
  theme_sc(axis_title_size = 12, base_size = 12) +
  scale_color_manual(values = c("grey60", "darkorange2")) +
  scale_alpha_manual(values = c(.3, .8), guide = FALSE) +
  ggtitle(NULL) +
  xlab(x_lab) +
  ylab("Proportion borrowed") +
  theme(
    axis.text = element_text(size = rel(.9)),
    panel.grid.minor = element_blank(),
    panel.grid.major.x = element_blank(),
    legend.position = c(0.7, 0.92),
    legend.margin = margin(t = 0, b = 0, unit = 'cm'),
    legend.background = element_rect(fill = "gray95", color = NA),
    legend.title = element_blank(),
    legend.direction  = "vertical",
    legend.text = element_text(size = rel(1.0))
    ) +
  guides(color = guide_legend(reverse = TRUE))


graphics.off()
windows(4, 4)
scatter_plot



# SMOOTHED LINE PLOT ---------------------
smooth_plot <- df_new  %>%
  filter(same_state == 0)  %>%
  filter(state_code_from != "USA")  %>%
  filter(state_code_to != "USA")  %>%
  ggplot(aes(x = scale(distance), y = ratio)) +
  geom_smooth(color = "grey30", size = 1.5, method = "loess") +
  theme_sc(axis_title_size = 12, base_size = 12) +
  scale_y_continuous(breaks = c(0, .002, .004)) +
  ggtitle(NULL) +
  xlab(x_lab) +
  ylab("Proportion borrowed") +
  theme(
    axis.text = element_text(size = rel(.9)),
    panel.grid.minor = element_blank(),
    legend.position = "top",
    legend.title = element_blank()
    )


graphics.off()
windows(4, 4)
smooth_plot




# SAVE ---------------

graphics.off()
windows(4, 4)
scatter_plot


ggsave("Plots/distance_scatter.png")

graphics.off()
windows(4, 4)
smooth_plot
ggsave("Plots/distance_smooth.png")

